<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/WEB-INF/jsp/taglib.jsp" %>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>我的钱包</title>

    <!-- Bootstrap -->
    <link href="/css/bootstrap.min.css" rel="stylesheet">
    <link href="/css/wallet.css" rel="stylesheet">



    <!--[if lt IE 9]>
    <script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- Web3 -->
    <script type="text/javascript" src="/js/web3.min.js"></script>
    <script type="text/javascript" src="/js/bignumber.js"></script>

    <script src="/js/jquery-2.2.3.min.js"></script>
    <script src="/js/bootstrap.min.js"></script>
    <script src="/js/wallet.js" type="text/javascript"></script>

    <script type="text/javascript">
        $(function () {
            // 初始化web3对象
            if (typeof web3 !== 'undefined') {
                web3 = new Web3(web3.currentProvider);
            } else {
                web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
            }

            // 判断是否连接
            var node = web3.isConnected();
            while (!node) {
                alert("无法连线，请在geth目录下根据使用手册启动节点！");
                node = web3.isConnected();
            }


            var EDUContract = web3.eth.contract([ { "constant": true, "inputs": [], "name": "name", "outputs": [ { "name": "", "type": "string", "value": "edu" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_spender", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "approve", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "totalSupply", "outputs": [ { "name": "", "type": "uint256", "value": "1000" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_from", "type": "address" }, { "name": "_to", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "transferFrom", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "decimals", "outputs": [ { "name": "", "type": "uint8", "value": "2" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_value", "type": "uint256" } ], "name": "burn", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [ { "name": "", "type": "address" } ], "name": "balanceOf", "outputs": [ { "name": "", "type": "uint256", "value": "0" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_from", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "burnFrom", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "symbol", "outputs": [ { "name": "", "type": "string", "value": "edu" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_to", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "transfer", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [ { "name": "_spender", "type": "address" }, { "name": "_value", "type": "uint256" }, { "name": "_extraData", "type": "bytes" } ], "name": "approveAndCall", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [ { "name": "", "type": "address" }, { "name": "", "type": "address" } ], "name": "allowance", "outputs": [ { "name": "", "type": "uint256", "value": "0" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "addr", "type": "address" } ], "name": "getBalance", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "name": "initialSupply", "type": "uint256", "index": 0, "typeShort": "uint", "bits": "256", "displayName": "initial Supply", "template": "elements_input_uint", "value": "1000" }, { "name": "tokenName", "type": "string", "index": 1, "typeShort": "string", "bits": "", "displayName": "token Name", "template": "elements_input_string", "value": "edu" }, { "name": "decimalUnits", "type": "uint8", "index": 2, "typeShort": "uint", "bits": "8", "displayName": "decimal Units", "template": "elements_input_uint", "value": "2" }, { "name": "tokenSymbol", "type": "string", "index": 3, "typeShort": "string", "bits": "", "displayName": "token Symbol", "template": "elements_input_string", "value": "edu" } ], "payable": false, "stateMutability": "nonpayable", "type": "constructor" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "from", "type": "address" }, { "indexed": true, "name": "to", "type": "address" }, { "indexed": false, "name": "value", "type": "uint256" } ], "name": "Transfer", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "from", "type": "address" }, { "indexed": false, "name": "value", "type": "uint256" } ], "name": "Burn", "type": "event" } ]);
            var EDU = EDUContract.at("0x4ca024FdA1fF2d4419d51f07D588add5D37b67ab");

            var myEvent = EDU.Transfer(null, {fromBlock: 0, toBlock: 'latest',address:'0x640be162b33e508bfac1a50970a5832ef4ee6eaa'});
            console.log(myEvent);


            var myResults = myEvent.get(function(error, logs){
                console.log(logs);
            });

            console.log(myResults);


            // 获取账户信息
            var accounts = web3.eth.accounts;
            $.ajax({
                url: "<spring:url value='/wallet/saveInfo' />",
                type: "post",
                contentType:"application/json",
                data: JSON.stringify(accounts),
                success: function (data) {

                }
            });

            var addressList = document.getElementById("accountInfo");
            var addressStr = "";

            for (var i = 0; i < accounts.length; i++) {
                var url = "<spring:url value='/wallet/accounts/" + accounts[i] + "'/>";
                var address = "<a href=" + url + ">" + accounts[i] + "</a>";

                addressStr = addressStr + "<tr>" +
                    "<td>" + (i + 1) + "</td>" +
                    "<td>" + address + "</td>" +
                    "<td>" + web3.fromWei(web3.eth.getBalance(accounts[i]).toNumber(), 'ether') + "</td>" +
                    "<td>" + EDU.getBalance.call(accounts[i]).toNumber() + "</td>" +
                    "</tr>";
            }
            addressList.innerHTML = addressStr;
        });
    </script>

    <c:import url="/WEB-INF/jsp/frontend/head.jsp" />
    <c:import url="/WEB-INF/jsp/frontend/activity/script.jsp" />
</head>
<body>

<div class="head">
    <c:import url="/WEB-INF/jsp/frontend/header.jsp"/>
</div>

<div class="container-wrap wallet-body">
    <div class="container">
        <div class="row account-container">
            <div class="col-md-9 account-selector">
                <h3>账户列表
                    <small>Accounts are password protected keys that can hold either.</small>
                </h3>
                <br><br>
                <table class="table table-hover table-bordered">
                    <thead>
                    <tr>
                        <th>#</th>
                        <th>账户地址</th>
                        <th>账户余额(Ether)</th>
                        <th>账户余额(EDU)</th>
                    </tr>
                    </thead>
                    <tbody id="accountInfo">
                    </tbody>
                </table>
            </div>
            <div class="col-md-3 wallet-info">
                <img class="img-circle wallet-portrait" src="/images/wallet/wallet-portrait.png">
                <span class="wallet-h1 wallet-username">${username}</span>
            </div>
        </div>
    </div>
</div>
</body>
</html>